﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Domain.Repositories;
using YAPACS.BusinessObjects.Exams;

namespace YAPACS.RisServer.Controllers;

[Authorize]
[ApiController]
public class PanicValueController(IRepository<Exam, Guid> examRepository) : AbpControllerBase
{
    private readonly IRepository<Exam, Guid> _examRepository = examRepository;
    [AllowAnonymous]
    [HttpPost("api/receive-panic-value")]
    public async Task<IActionResult> ReceivePanicValue([FromBody] PanicValueInput input)
    {
        //申请单号 和检查项目ID
        if (string.IsNullOrWhiteSpace(input.ApplyNumber) && string.IsNullOrWhiteSpace(input.ExamItemNumber))
        {
            throw new Exception("请填写【申请单号】或【检查项目编号】");
        }
        var exams = await _examRepository.GetListAsync(e => e.Report != null && e.Report.ReportPanicValue != null && (e.ApplyNumber == input.ApplyNumber || e.ExamApplyItems.Any(f => input.ExamItemNumber == f.ExamItemNumber)));
        foreach (var exam in exams)
        {
            exam.Report.ReportPanicValue.ReceivingTime = Clock.Now;
        }
        return Ok(new { Success = true });
    }

    public class PanicValueInput
    {
        /// <summary>
        /// 申请单号
        /// </summary>
        public string ApplyNumber { get; set; }
        /// <summary>
        /// 检查项目编号
        /// </summary>
        public string ExamItemNumber { get; set; }
    }
}
